package com.feng.sharedingjdbc.service.impl;

import com.feng.sharedingjdbc.dao.OrderRepository;
import com.feng.sharedingjdbc.mapper.OrderEntityMapper;
import com.feng.sharedingjdbc.model.OrderEntity;
import com.feng.sharedingjdbc.rpc.UserService;
import com.feng.sharedingjdbc.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author yinglong.zhang
 * @description
 * @date 2024-1-29 13:56
 */

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {


    private final OrderRepository repository;
    private final OrderEntityMapper orderEntityMapper;
    private final UserService userService;


    public OrderServiceImpl(OrderRepository repository, OrderEntityMapper orderEntityMapper, UserService userService) {
        this.repository = repository;
        this.orderEntityMapper = orderEntityMapper;
        this.userService = userService;
    }


    @Override
    public OrderEntity insert(OrderEntity orderEntity)  {
        int insert = orderEntityMapper.insert(orderEntity);
        if (insert <= 0) {
            throw new RuntimeException("保存失败");
        }
        return orderEntity;
    }

    @Override
    public OrderEntity update(OrderEntity orderEntity) {
        return repository.save(orderEntity);
    }

    @Override
    public boolean deleteById(Long id) {
        boolean result = true;
        try {
            repository.deleteById(id);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    public Optional<OrderEntity> queryById(Long id) {
        return repository.findById(id);
    }

}